﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Project01
{
    class Player:ObjectGame //thông số kủa player
    {
        public float Speed;
        //public byte healh;

        public List<Bullets> bullets = new List <Bullets>();
        private TimeSpan TimeBullets;
        protected string BulletAssetname;
        protected int BulletRow, BulletCol;
        private byte Damage;
        private TimeSpan Timeplayer; // thời gian chờ lúc bắt đầu game
         private int KeyState;
        protected KeyboardState key;
        private Game game;
        public Player(Game game)
            : base(game)
        {
            ObjectTexture = game.Content.Load<Texture2D>("Images/Player/Player");
            Sprite = new AnimationSprite(ObjectTexture,3,9);
            Sprite.Position = new Vector2(400, 300);
            Speed = 5f;
            Damage = 1;
            this.game = game;
            
        }
        //public virtual void LoadContent() { }
        public override void Update(GameTime gameTime)
        {
            Timeplayer += gameTime.ElapsedGameTime;
       
            KeyState = 0;
            if (Keyboard.GetState().IsKeyDown(Keys.Up))
                KeyState = 1;
            if (Keyboard.GetState().IsKeyDown(Keys.Down))
                KeyState = 2;
            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                KeyState = 3;
            if (Keyboard.GetState().IsKeyDown(Keys.Right))
                KeyState = 4;
            if (Keyboard.GetState().IsKeyDown(Keys.Up) && Keyboard.GetState().IsKeyDown(Keys.Left))
                KeyState = 5;
            if (Keyboard.GetState().IsKeyDown(Keys.Up) && Keyboard.GetState().IsKeyDown(Keys.Right))
                KeyState = 6;
            if (Keyboard.GetState().IsKeyDown(Keys.Down) && Keyboard.GetState().IsKeyDown(Keys.Left))
                KeyState = 7;
            if (Keyboard.GetState().IsKeyDown(Keys.Down) && Keyboard.GetState().IsKeyDown(Keys.Right))
                KeyState = 8;

            switch (KeyState)
            {
                case 0:

                        Sprite.Update(gameTime, 9, 11, 100);

                    break;

                case 1: //Up
                    Sprite.Position.Y -= Speed;

                    if (Sprite.Position.Y < 0)
                        Sprite.Position.Y = 0;

                        Sprite.Update(gameTime, 18, 20, 100);

                    break;

                case 2: //Down
                    Sprite.Position.Y += Speed;

                    if (Sprite.Position.Y > game.Window.ClientBounds.Height - Sprite.RecHeight)
                        Sprite.Position.Y = game.Window.ClientBounds.Height - Sprite.RecHeight;

                        Sprite.Update(gameTime, 0, 0, 100);

                    break;
                case 3: // Left
                    Sprite.Position.X -= Speed;

                    if (Sprite.Position.X < 0)
                        Sprite.Position.X = 0;

                        Sprite.Update(gameTime, 12, 14, 100);

                    break;
                case 4: //Right
                    Sprite.Position.X += Speed;

                    if (Sprite.Position.X > game.Window.ClientBounds.Width - Sprite.RecWidth)
                        Sprite.Position.X = game.Window.ClientBounds.Width - Sprite.RecWidth;

                    Sprite.Update(gameTime, 15, 17, 100);

                    break;

                case 5: // Up Left
                    Sprite.Position.Y -= Speed;
                    Sprite.Position.X -= Speed;

                    if (Sprite.Position.Y < 0)
                        Sprite.Position.Y = 0;
                    if (Sprite.Position.X < 0)
                        Sprite.Position.X = 0;

                    Sprite.Update(gameTime, 21, 23, 100);

                    break;
                case 6: // Up Right
                    Sprite.Position.Y -= Speed;
                    Sprite.Position.X += Speed;

                    if (Sprite.Position.Y < 0)
                        Sprite.Position.Y = 0;
                    if (Sprite.Position.X > game.Window.ClientBounds.Width - Sprite.RecWidth)
                        Sprite.Position.X = game.Window.ClientBounds.Width - Sprite.RecWidth;

                    Sprite.Update(gameTime, 24, 26, 100);

                    break;
                case 7: // Down Left
                    Sprite.Position.Y += Speed;
                    Sprite.Position.X -= Speed;

                    if (Sprite.Position.Y > game.Window.ClientBounds.Height - Sprite.RecHeight)
                        Sprite.Position.Y = game.Window.ClientBounds.Height - Sprite.RecHeight;
                    if (Sprite.Position.X < 0)
                        Sprite.Position.X = 0;

                    Sprite.Update(gameTime, 1, 1, 100);

                    break;
                case 8: // Down Right
                    Sprite.Position.Y += Speed;
                    Sprite.Position.X += Speed;

                    if (Sprite.Position.Y > game.Window.ClientBounds.Height - Sprite.RecHeight)
                        Sprite.Position.Y = game.Window.ClientBounds.Height - Sprite.RecHeight;
                    if (Sprite.Position.X > game.Window.ClientBounds.Width - Sprite.RecWidth)
                        Sprite.Position.X = game.Window.ClientBounds.Width - Sprite.RecWidth;

                    Sprite.Update(gameTime, 2, 2, 100);

                    break;
            }

            TimeBullets += gameTime.ElapsedGameTime;
            UpdateBullets(gameTime);

            if (Keyboard.GetState().IsKeyDown(Keys.Space))
            {
                if (TimeBullets >= TimeSpan.FromSeconds(0.2))
                {
                    Shoot();
                    TimeBullets = TimeSpan.Zero;
                }
            }

        }


        private void UpdateBullets(GameTime gameTime)
        {
            for (int i = 0; i < bullets.Count; i++)
            {
                bullets[i].Sprite.Position -= new Vector2(0, Speed * 2);
                bullets[i].Sprite.Update(gameTime, 0, 5, 30);
                if (bullets[i].Sprite.Position.Y <= 0 - bullets[i].Sprite.RecHeight)
                    bullets.RemoveAt(i);

            }
        }

        private void Shoot()
        {

            Bullets newBullet = new Bullets(game, "Images/Bullets/Player/B_P_Type01", 1, 6);
            newBullet.Damage = Damage;
            newBullet.Sprite.Position = new Vector2(Sprite.Position.X + (Sprite.RecWidth / 2 - newBullet.Sprite.RecWidth / 2), Sprite.Position.Y);
            bullets.Add(newBullet);
        } 


        public override void Draw(SpriteBatch SpriteBatch)
        {
            this.Sprite.Draw(SpriteBatch);
            for (int i = 0; i < bullets.Count; i++)
                bullets[i].Draw(SpriteBatch);
            base.Draw(SpriteBatch);
        }
    }
}
